# -*- encoding: utf8 -*-

DAILY_USER_REPORT_V2 = {
    'dynamic_argument': True,
    'group_by': '_chn.keyword',
    'target_event_id': ['recharge', 'withdraw', 'active', 'register', 'bind', 'bet_order'],
    'values': [
        {
            "value_id": "new_device",
            "target_value": "_aid.keyword",
            "value_name": "新增设备",
            "filter": [
                {'term': {'_event_id': 'register'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_user",
            "target_value": "_user_id",
            "value_name": "注册帐号",
            "filter": [
                {'term': {'_event_id': 'register'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "device_transform_rate",
            "value_name": "账号转化率",
            "value_expr": "new_user/new_device",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },


        {
            "value_id": "new_register_device",
            "target_value": "_aid.keyword",
            "value_name": "新绑定设备",
            "filter": [
                {'term': {'_event_id': 'bind'}},
                {'term': {"_is_new_user": 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_user",
            "target_value": "_user_id",
            "value_name": "新绑定账号 ",
            "filter": [
                {'term': {'_event_id': 'bind'}},
                {'term': {"_is_new_user": 1}}
            ],
            "operation": "cardinality",
        },


        {
            "value_id": "register_rate",
            "value_name": "注绑率",
            "value_expr": "new_register_user/new_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "device_stay_2",
            "target_value": "_aid.keyword",
            "value_name": "设备次留",
            "filter": [
                {"term": {"_event_id": "active"}},
                {"term": {"_device_active_days": 1}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
            "value_type": "long"
        },
        {
            "value_id": "user_stay_2",
            "target_value": "_user_id",
            "value_name": "账号次留",
            "filter": [
                {"term": {"_event_id": "active"}},
                {"term": {"_active_days": 1}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
            "value_type": "long"
        },
        {
            "value_id": "total_register_user",
            "target_value": "_user_id",
            "value_name": "总绑定账号",
            "filter": [
                {'term': {'_event_id': 'bind'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_recharge_device",
            "target_value": "_aid.keyword",
            "value_name": "新付费设备",
            "filter": [
                {'term': {"_event_id": "recharge"}},
                {"term": {"_is_new_user": 1}}
            ],
            "operation": "cardinality"
        },
        {
            "value_id": "new_recharge_user",
            "target_value": "_user_id",
            "value_name": "新付费账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_recharge_ratio",
            "value_name": "新注充率",
            "value_expr": "new_recharge_user/new_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "new_bind_recharge_ratio",
            "value_name": "新绑充率",
            "value_expr": "new_recharge_user/new_register_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "new_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "新付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
            "value_type": "float"
        },
        {
            "value_id": "new_user_arpu",
            "value_name": "新用户Arpu",
            "value_expr": "new_recharge_amount/new_user"
        },
        {
            "value_id": "new_user_arppu",
            "value_name": "新用户Arppu",
            "value_expr": "new_recharge_amount/new_recharge_user"
        },
        {
            "value_id": "first_recharge_user",
            "target_value": "_user_id",
            "value_name": "首充账号",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "operation": "cardinality"
        },
        {
            "value_id": "first_recharge_count",
            "target_value": "recharge_price",
            "value_name": "首充金额",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "value_type": "float"
        },
        {
            "value_id": "active_device",
            "target_value": "_aid.keyword",
            "value_name": "活跃设备",
            "filter": [
                {'term': {'_event_id': 'active'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "active_user",
            "target_value": "_user_id",
            "value_name": "活跃用户",
            "filter": [
                {'term': {'_event_id': 'active'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_device",
            "target_value": "_aid.keyword",
            "value_name": "付费设备",
            "filter": [
                {"term": {"_event_id": "recharge"}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_user",
            "target_value": "_user_id",
            "value_name": "付费账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_rate",
            "value_name": "付费率",
            "value_expr": "recharge_user/active_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "recharge_amount",
            "target_value": "recharge_price",
            "value_name": "付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "value_type": "float"
        },
        {
            "value_id": "arpu",
            "value_name": "Arpu",
            "value_expr": "recharge_amount/new_user"
        },
        {
            "value_id": "arppu",
            "value_name": "Arppu",
            "value_expr": "recharge_amount/recharge_user"
        },
        {
            "value_id": "withdraw_user",
            "target_value": "_user_id",
            "value_name": "兑换账号",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "withdraw_amount",
            "target_value": "withdraw_real_price",
            "value_name": "兑换金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ],
            "value_type": "float"
        },
        {
            "value_id": "profit_amount",
            "target_value": ["recharge_price", "-", "withdraw_real_price"],
            "value_name": "营收金额",
            "filter": [
                {'terms': {'_event_id': ['recharge', 'withdraw']}},
            ],
            "value_type": "float"
        },
        {
            "value_id": "game_profit_amount",
            "target_value": ["bet_order_bet_amount", "-", "bet_order_award_amount"],
            "value_name": "玩法营收金额",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}},
            ],
            "value_type": "float"
        }
    ]
}